home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / filesy~1 / mfs609s.zoo / fsck / common.c next >
Encoding:
C/C++ Source or Header  |  1993-11-25  |  5.6 KB  |  318 lines

  1. /* This file is part of 'fsck' Copyright S.N. Henson */
  2.  
  3. /* Functions Common to V1 and V2 Filesystems */
  4.  
  5. #include <stdio.h>
  6. #include <alloc.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include "fs.h"
  10. #include "global.h"
  11. #include "proto.h"
  12.  
  13. /* Parse a comma separated list of integers, return number of integers read.
  14.  * Fill in elements in a linked list. 
  15.  */
  16.  
  17. int comma_parse(str,list)
  18. char *str;
  19. llist **list;
  20. {
  21.     char *tstr,*fstr,*comma;
  22.     int count;
  23.     count=0;
  24.     if( !(tstr=strdup(str)) ) fatal("Out of memory");
  25.     fstr=tstr;
  26.  
  27.     do
  28.     {
  29.         llist *p;
  30.         comma=strchr(tstr,',');
  31.         if(comma) *comma=0;
  32.         if( !(p=malloc(sizeof(list)) ) ) fatal("Out of memory");
  33.         p->member=atol(tstr);        
  34.         p->next=*list;
  35.         *list=p;
  36.         count++;
  37.         if(comma) tstr=++comma;
  38.     }
  39.     while(comma);
  40.  
  41.     free(fstr);
  42.  
  43.     return count;
  44. }
  45.  
  46. void read_zone(zone,buff)
  47. long zone;
  48. void *buff;
  49. {
  50.     read_blocks(zone,1,buff);
  51. }
  52.  
  53. void write_zone(zone,buff)
  54. long zone;
  55. void *buff;
  56. {
  57.     if(zone==0)
  58.     {
  59.         printf("Illegal Write Zone\n");
  60.         return;
  61.     }
  62.     write_blocks(zone,1,buff);
  63. }
  64.  
  65. /* Check a zone number is legitimate */
  66. int chk_range(zone)
  67. long zone;
  68. {
  69.     if( (zone > maxzone) || (zone < minzone) ) return 0;
  70.     return 1;
  71. }
  72.  
  73.  
  74. /* Bitmap stuff */
  75.  
  76. void setbit(zone,map)
  77. long zone;
  78. unsigned *map;
  79. {
  80.     map[zone>>4] |= (1<<(zone & 0xf));
  81. }
  82.  
  83. void clrbit(zone,map)
  84. long zone;
  85. unsigned *map;
  86. {
  87.     map[zone>>4] &= ~(1<<(zone & 0xf));
  88. }
  89.  
  90. int isset(zone,map)
  91. long zone;
  92. unsigned *map;
  93. {
  94.     return( ( map[zone>>4] & ( 1<<(zone & 0xf) ) ) ? 1:0 );
  95. }
  96.  
  97. /* Find the first Zero bit in bitmap */
  98. long findbit(map,limit)
  99. unsigned *map;
  100. long limit;
  101. {
  102.     long ret;
  103.     long i;
  104.     int j;
  105.  
  106.     for(i=0;i<(limit+15/16);i++)
  107.     {
  108.         if(map[i]!=0xffff)
  109.         {
  110.             for(j=0;j<16;j++) 
  111.             {
  112.                 if(!(map[i] & (1<<j)))
  113.                 {
  114.                             
  115.                     ret=i*16+j;
  116.                     if(ret < limit)
  117.                     {
  118.                         setbit(ret,map);
  119.                         return ret;
  120.                     }
  121.                     return 0;
  122.                 }
  123.             }
  124.         }
  125.     }
  126.     return 0;
  127. }
  128.  
  129. long alloc_zone()
  130. {
  131.     long ret;
  132.     ret=findbit(szbitmap,maxzone-minzone);
  133.     if(!ret) return 0;
  134.     ret+=minzone-1;
  135.     return ret;
  136. }
  137.  
  138. int mark_zone(zone)
  139. long zone;
  140. {
  141.     unsigned ret;
  142.     zone-=minzone-1;
  143.     ret= isset(zone,szbitmap);
  144.     setbit(zone,szbitmap);
  145.     return ret;
  146. }
  147.  
  148. void unmark_zone(zone)
  149. long zone;
  150. {
  151.     zone-=minzone-1;
  152.     clrbit(zone,szbitmap);
  153. }
  154.  
  155. int ask(str,alt)
  156. char *str;
  157. char *alt;
  158. {
  159.     char ans[20];
  160.     if(alln) return 0;
  161.     if(ally)
  162.     {
  163.         printf("(%s)",alt);
  164.         return 1;
  165.     }
  166.     printf("%s",str);
  167.     fgets(ans,20,stdin);
  168.     if( (ans[0] & ~32) == 'Y') return 1;
  169.     return 0;
  170. }
  171.  
  172. void fatal(str)
  173. char *str;
  174. {
  175.     printf("Fatal error: %s\n",str);
  176.     close_device();
  177.     exit(1);
  178. }
  179.  
  180. void sfatal(str)
  181. char *str;
  182. {
  183.     printf("Bad Superblock: %s\n",str);
  184.     close_device();
  185.     exit(1);
  186. }
  187.  
  188.  
  189. void usage()
  190. {
  191.     printf("Usage : fsck device\n");
  192.     printf("Filesystem consistency checker. Copyright S.N. "
  193.     "Henson 1992,1993.\nAll Rights Reserved\n");
  194.     printf("Version 0.0 pre-alpha patchlevel 5\n");
  195.     exit(1);
  196. }
  197.  
  198. /* Read in superblock, perform some sanity checks on it, then read in the 
  199.  * bitmaps.
  200.  */
  201.  
  202. void read_tables()
  203. {
  204.     long expect;
  205.     Super=(super_block *)malloc(BLOCKSIZE);
  206.     read_zone(1,Super);
  207.     /* Sanity checks on super block */
  208.     if(Super->s_magic!=SUPER_V1)
  209.     {
  210.  
  211.         if(Super->s_magic==SUPER_V2) 
  212.         {
  213.             version=1;
  214.             maxzone=Super->s_zones;
  215.         }
  216.         else sfatal("Invalid Magic Number");
  217.     }
  218.     else maxzone=Super->s_nzones;
  219.  
  220.     if(set_size(maxzone)) fatal("Cannot access filesystem");
  221.  
  222.     expect=(Super->s_ninodes+8192l)/8192;
  223.  
  224.     if( expect > Super->s_imap_blks)
  225.         sfatal("Inode Bitmap too Small");
  226.     if( expect < Super->s_imap_blks) fprintf(stderr,"Warning: inode bitmap"
  227.              "%d blocks, expected %ld\n",Super->s_imap_blks,expect);
  228.     expect=(maxzone+8191)/8192;
  229.     if(expect > Super->s_zmap_blks )
  230.         sfatal("Zone Bitmap too Small");
  231.     if(expect < Super->s_zmap_blks ) fprintf(stderr,"Warning: zone bitmap"
  232.             "%d blocks, expected %ld\n",Super->s_zmap_blks,expect);
  233.     /* Set up some variables */
  234.  
  235.     maxino=Super->s_ninodes;
  236.     minzone=Super->s_firstdatazn;
  237.     maxzone--;
  238.  
  239. }
  240.  
  241. int do_trunc()
  242. {
  243.     printf("Inode %ld Contains Too Many Zones\n",cino);
  244.     done_trunc=1;
  245.     if(ask("Truncate?","Truncated"))
  246.     {
  247.         trunc=1;
  248.         return 1;
  249.     }
  250.     return 0;
  251. }
  252.  
  253.  
  254. int chk_irange(inum)
  255. unsigned inum;
  256. {
  257.     if(inum && inum <=maxino) return 1;
  258.     return 0;
  259. }
  260.  
  261.  
  262. void inerr(name)
  263. char *name;
  264. {
  265.     printf("Inode %ld: %s\n",cino,name);
  266. }
  267.  
  268. int badname(name)
  269. char *name;
  270. {
  271.     unsigned short n = MMAX_FNAME(incr);
  272.     if(!*name) return 1;
  273.     while(n-- && *name ) if( *name++ == '/' ) return 1;
  274.     return 0;
  275. }
  276.  
  277. /* Copy a directory entry */
  278. void cpdir(dest,src)
  279. dir_struct *dest,*src;
  280. {
  281.     bcopy((char*)src,(char *)dest,DSIZE*incr);
  282. }
  283.  
  284. /* Show FS info */
  285. void showinfo()
  286. {
  287.     if(info)
  288.     {
  289.         printf("\nDrive %s\n",drvnam);
  290.         printf("V%d Filesystem\n",version+1);
  291.         printf("Directory Increment %d\n",incr);
  292.         printf("%ld\t\tInodes\n",maxino);
  293.         printf("%ld\t\tFree Inodes\n",ifree);
  294.         printf("%ld\t\tData Zones\n",maxzone-minzone+1);
  295.         printf("%ld\t\tFree Data Zones\n",zfree);
  296.         printf("%ld\t\tDirectories\n",ndir);
  297.         printf("%ld\t\tRegular Files\n",nreg);
  298.         printf("%ld\t\tSymbolic Links\n\n",nsym);
  299. #ifdef NOTUSED
  300.         printf("%ld\t\tFIFO's\n",nfifo);
  301.         printf("%ld\t\tCharacter Special Files\n",nchar);
  302.         printf("%ld\t\tBlock Special Files\n",nblk);
  303. #endif
  304.     }
  305.  
  306.     if(info>1)
  307.     {
  308.         printf("First Inode Block  %ld\n",ioff);
  309.         printf("First Datazone     %ld\n\n",minzone);
  310.     }
  311.     if(modified) 
  312.     {
  313.         printf("**************************************\n");
  314.         printf("*    FILESYSTEM HAS BEEN MODIFIED    *\n");
  315.         printf("**************************************\n\n");
  316.     }
  317. }
  318.